/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.property;

import org.openaecfm.model.property.ComplexProperty;
import org.openaecfm.model.property.Property;
import org.openaecfm.service.property.IComplexPropertyManager;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class ComplexPropertyIO extends
		AbstractEntityInstanceInputImpl<ComplexProperty> implements
		IEntityInstanceOutput<ComplexProperty> {
	private IEntityInstanceUtilInput<Property> utilInput;
	private IEntityInstanceUtilOutput<Property> utilOutput;
	private IComplexPropertyManager manager;

	public void setManager(IComplexPropertyManager manager) {
		this.manager = manager;
	}

	public void setUtilOutput(IEntityInstanceUtilOutput<Property> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(ComplexProperty pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCCOMPLEXPROPERTY", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set UsageName
		StringParameter usagename = new StringParameter(pojo.getUsageName(),
				StringParameter.UTF_8);
		ei.addParameter(usagename);

		// Set HasProperties
		ListParameter hasps = new ListParameter();
		for (Property p : pojo.getHasProperties()) {
			EntityInstanceNameParameter n = new EntityInstanceNameParameter(
					appDataManager.getName(p));
			hasps.add(n);
		}
		ei.addParameter(hasps);

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected ComplexProperty doCreateObject() {
		return new ComplexProperty();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			ComplexProperty pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set HasProperties
		ListParameter hasps = (ListParameter) sei.getParameter(3);
		for (Parameter p : hasps) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) p;
			manager.addHasProperty(pojo,
					(Property) appDataManager.getObject(n.toInstanceName()));
		}
	}

	public void setUtilInput(IEntityInstanceUtilInput<Property> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			ComplexProperty pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set UsageName
		StringParameter usagename = (StringParameter) sei.getParameter(2);
		pojo.setUsageName(usagename.toUTF8());
	}
}
